package cn.okcode.api.framework.dispatcher;

import cn.hutool.core.convert.Convert;
import cn.okcode.api.framework.ActionException;
import cn.okcode.api.framework.Callee;
import cn.okcode.api.framework.Config;
import cn.okcode.api.framework.Jsp2ApiException;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 默认的处理器获取类
 *
 * @author pyk13
 */
public class DefaultHandlerResolver implements IHandlerResolver {
    private static final Logger logger = LogManager.getLogger(DefaultHandlerResolver.class.getName());

    @Override
    public Callee findHandler(Map<String, Callee> handlerMappings, HttpServletRequest request) {
        if (handlerMappings.isEmpty()) {
            throw new ActionException("没有找到任何可用请求处理器！");
        }
        String controllerName = request.getParameter(JspApiDispatcher.JS_CONTROLLER_SUFFIX);
        if (logger.isDebugEnabled()) {
            logger.debug("-----_controller-----1:" + controllerName);
        }
        if (StringUtils.isBlank(controllerName)) {
            throw new ActionException("controller不能为空");
        }

        String actionName = request.getParameter("_action");
        if (logger.isDebugEnabled()) {
            logger.debug("-----_action-------1:" + actionName);
        }
        if (StringUtils.isBlank(actionName)) {
            throw new ActionException("action不能为空");
        }
        String callName = controllerName + "." + actionName;
        for (String packageName : Config.CONTROLLER_PACKAGE_NAME) {
            String fullCallName = packageName + "." + controllerName + "Controller." + actionName;
            Callee callee = handlerMappings.get(fullCallName);
            if (callee != null) {
                return callee;
            }
        }
        throw new ActionException("没有找到" + callName + "对应的处理器！");
    }

    @Override
    public Object[] findParameterValues(Callee action, HttpServletRequest request) {
        ParamDefinition[] parameters = action.getParamDefinitions();
        Object[] parameterValues = new Object[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            ParamDefinition parameter = parameters[i];
            String strValue = request.getParameter(parameter.getName());
            if (parameter.isRequired() && StringUtils.isBlank(strValue)) {
                throw new Jsp2ApiException(parameter.getBlankText());
            }
            //请求对象特殊处理
            if (parameter.getParameterType().equals(HttpServletRequest.class)) {
                parameterValues[i] = request;
            } else if (parameter.isSimpleType()) {
                parameterValues[i] = Convert.convert(parameter.getParameterType(), strValue);
            } else {
                if (parameter.getParameterizedType() != null) {
                    parameterValues[i] = JSON.parseObject(strValue, parameter.getParameterizedType());
                } else {
                    parameterValues[i] = JSON.parseObject(strValue, parameter.getParameterType());
                }
            }
        }
        return parameterValues;
    }
}
